home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / Dual / Source / DualView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.0 KB  |  77 lines

  1. // DualView.m
  2.  
  3. #import "DualView.h"
  4. #import "Controller.h"
  5. #import <stdio.h>
  6.  
  7. @implementation DualView
  8.  
  9. - mouseDown: (NXEvent *)event
  10. {
  11.     NXPoint mouseLocation;
  12.     
  13.     mouseLocation = event->location;
  14.     [self convertPoint:&mouseLocation fromView:nil];
  15.     mouseLocation.x = xMin + mouseLocation.x / bounds.size.width * (xMax - xMin);
  16.     mouseLocation.y = yMin + mouseLocation.y / bounds.size.height * (yMax - yMin);
  17.     [controller addDual:mouseLocation];
  18.     return self;
  19. }
  20.  
  21. #define X(x) ((x - xMin) * sx)
  22. #define Y(y) ((y - yMin) * sy)
  23.  
  24. - drawSelf:(const NXRect *)rects :(int)rectCount
  25. {
  26.     PointLine *primalPoint, *primalLine;
  27.     int numberOfPrimalPoints, numberOfPrimalLines;
  28.     int i;
  29.     float sx = bounds.size.width / (xMax - xMin);
  30.     float sy = bounds.size.height / (yMax - yMin);
  31.     
  32.     [super drawSelf:rects :rectCount];
  33.         
  34.     // draw dual lines (from primal points)
  35.     // primal line y = ax - b <=> (a,b)
  36.     [controller getPrimalPoints:&numberOfPrimalPoints :&primalPoint];
  37.     for (i = 0; i < numberOfPrimalPoints; i++) {
  38.         float x = xMin;
  39.         float y = x * primalPoint[i].a - primalPoint[i].b;
  40.         int dy = 0;        // used to adjust the position of the line label
  41.         if (y < yMin && primalPoint[i].a) {
  42.             y = yMin;
  43.             x = (y + primalPoint[i].b) / primalPoint[i].a;
  44.         } else if (y > yMax && primalPoint[i].a) {
  45.             y = yMax;
  46.             x = (y + primalPoint[i].b) / primalPoint[i].a;
  47.             dy = -12;
  48.         }
  49.         NXSetColor(primalPoint[i].color);
  50.         if ([showLabels state]) {
  51.             PSmoveto(X(x), Y(y) + dy);
  52.             PSshow(primalPoint[i].label);
  53.         }
  54.         PSmoveto(X(x), Y(y));
  55.         PSlineto(X(xMax), Y(xMax * primalPoint[i].a - primalPoint[i].b));
  56.         PSstroke();
  57.     }
  58.  
  59.     // draw dual point (from primal lines)
  60.     // primal point (a,b) <=> y = ax - b
  61.     [controller getPrimalLines:&numberOfPrimalLines :&primalLine];
  62.     for (i = 0; i < numberOfPrimalLines; i++) {
  63.         NXSetColor(primalLine[i].color);
  64.         if ([showLabels state]) {
  65.             PSmoveto(X(primalLine[i].a), Y(-primalLine[i].b));
  66.             PSshow(primalLine[i].label);
  67.         }
  68.         PSnewpath();
  69.         PSarc(X(primalLine[i].a), Y(-primalLine[i].b), 2.0, 0.0, 360.0);
  70.         PSfill();
  71.     }
  72.     
  73.     return self;
  74. }
  75.  
  76. @end
  77.